package logik;

import java.sql.SQLException;
import javax.swing.JOptionPane;

/**
 * @author Matthias
 * Represents a withdrawal ATM transaction
 */
public class Withdrawal extends Transaction {
	
	public Withdrawal (PerformTransactions perform) {
		super (perform);
	}

	@Override
	public void execute() {

		boolean cashDispensed = false; // cash was not dispensed yet
		
		double withdrawalCustomAmount = 0;
		withdrawalCustomAmount = getClassPerform().getClassATM().getClassScreen().
				getAccWithdrawal().getWithdrawInputOfDialog();
		
		double withdrawalRadioBtnAmount = 0;
		withdrawalRadioBtnAmount = getClassPerform().getClassATM().getClassScreen().
				getAccWithdrawal().getWithdrawRdbtnAmount();
		
		double withdrawalSum = withdrawalRadioBtnAmount + withdrawalCustomAmount;

		// check whether the user has enough money in the account 
		if (withdrawalSum <= getTotBal()) { 
				try {
					getClassPerform().getClassATM().getSubBdbAccSQL().debitAccSQL(getAccNr(), withdrawalSum);	
					cashDispensed = true; // cash was dispensed
				} catch (SQLException e) {
					JOptionPane.showMessageDialog(null, "Error", 
							"Error!", JOptionPane.ERROR_MESSAGE);	
				}
		} // end if
		else { // cash dispenser does not have enough cash
			JOptionPane.showMessageDialog(null, "Insufficient cash available in the ATM." +
				"\n\nPlease choose a smaller amount.", 
				"Error!", JOptionPane.ERROR_MESSAGE);	
		} // end else
	} // end method

} // end class Withdrawal
